package org.thingsboard.server.config.annotation;


        import com.fasterxml.jackson.databind.ObjectMapper;
        import com.google.gson.Gson;
        import lombok.SneakyThrows;
        import org.apache.commons.lang3.StringUtils;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Component;
        import org.thingsboard.server.common.data.exception.ThingsboardErrorCode;
        import org.thingsboard.server.utils.AjaxResult;

        import javax.servlet.*;
        import javax.servlet.http.HttpServletRequest;
        import java.io.BufferedReader;
        import java.io.IOException;
        import java.io.InputStreamReader;
        import java.io.OutputStream;
        import java.math.BigDecimal;
        import java.util.Map;

@Component
        public class HttpServletRequestReplacedFilter implements Filter {
    /**
     * 秘钥
     */
    private static final String SECRET= "app-zhjs";

    @Autowired
    private ObjectMapper mapper;
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {

        }
    private static final Gson gson = new Gson();
        @SneakyThrows
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)  {
            ServletRequest requestWrapper = null;
            if(request instanceof HttpServletRequest) {
                requestWrapper = new RequestWrapper((HttpServletRequest) request);
            }
            BufferedReader streamReader = new BufferedReader( new InputStreamReader(requestWrapper.getInputStream(), "UTF-8"));
            StringBuilder responseStrBuilder = new StringBuilder();
            String inputStr;
            while ((inputStr = streamReader.readLine()) != null) {
                responseStrBuilder.append(inputStr);
            }
            HttpServletRequest httpServletRequest= (HttpServletRequest) request;
            Map<String,Map> data=gson.fromJson(responseStrBuilder.toString(),Map.class);
            System.out.println(httpServletRequest.getRequestURI());
            if(httpServletRequest.getRequestURI().contains("websocket")){
                chain.doFilter(requestWrapper, response);
                return;
            }
            /*if(httpServletRequest.getRequestURI().contains("iot-web")){
                Map map=data.get("requestHead");
                String sign=Demo.encryptSHA(httpServletRequest.getMethod(),httpServletRequest.getRequestURI(),map.get("systemCode")+"",new BigDecimal(map.get("timestamp")+"").toBigInteger().longValue(), new BigDecimal(map.get("nonce")+"").toBigInteger().intValue(),map.get("systemCode")+"");
                if(StringUtils.equals(sign, map.get("signature")+"")){
                    chain.doFilter(requestWrapper, response);
                }else {
                    response.setCharacterEncoding("UTF-8"); //设置编码格式
                    response.setContentType("application/json; charset=utf-8");
                    mapper.writeValue(response.getWriter(), AjaxResult.error(ThingsboardErrorCode.ERROR_4001));
                }
            }*/
            chain.doFilter(requestWrapper, response);
        }
        @Override
        public void destroy() {
        }

        }

